<?php
namespace app\common\tool;

class Getui extends Base {
	protected $model = 'tyyh\\Getui';

	//保存push_clientid
	public function save_push_clientid($uid, $push_clientid) {
		$model = loader()->loadModel($this->model);
		$row = $model->where(['uid' => $uid])->find();
		if($row) {
			$row->push_clientid = $push_clientid;
			$row->save();
		} else {
			$model->save([
				'uid'           => $uid,
				'push_clientid' => $push_clientid
			]);
		}
	}

	//推送
	public function push($uid, $content = '您有新的订单', $music = '您有新的订单,请及时处理') {
		$model = loader()->loadModel($this->model);
		$row = $model->where(['uid' => $uid])->find();
		$curl = loadTool('Curl');
		$data = [
			'push_clientid' => $row ? $row['push_clientid'] : '',  
			'title'         => '田园鲜仓',
			'content'       => $content,
			'payload' => [
				'music' => $music
			]
		];
		$result =  $curl->post('https://cloud.jxoxy.com/xwz', $data, 'json_encode');
		$result = json_decode($result, true);
		$result['param'] = $data;
		return $result;
	}
}
